﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;//添加命令空间，可以使用OleDbConnection等对象
using System.Data;//添加命令空间，可以使用dataset

public partial class adminstrator_findCourse : System.Web.UI.Page
{
    public string cID;
    private OleDbConnection myConnection;
    public static readonly string sConnectionString = System.Configuration.ConfigurationManager.AppSettings["SQLConnStriing"].ToString();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sConnectionString);
        myConnection.Open();//打开数据库连接
        if (Session["UserID"] == null && Session["UserName"] == null)//没有经过正常连接途径打开页面
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登陆！');parent.location.herf='../login.aspx'</script>", false);
        
    }
    cID=Request.QueryString["cID"];
    if (!IsPostBack )
    {
        bind(cID);
    }
}
    public void bind(string id)
    {
        string mysql = "select * from CourseTb  where cNo='"+ id+"'";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataReader myread=mycmd .ExecuteReader  ();
        if (myread.Read ())
        {
            TextBox1.Text = "";//清空提示信息
            TextBox1.Text = "";
            TextBox1.Text = "";
            TextBox1.Text = "";
            TextBox1.Text = "";
            TextBox1.Text = "";
            TextBox1.Text = "";
            TextBox1.Text = "";
        }
        else
        {
            TextBox1.Text = "无法查到相关数据！";
            GridView1.DataSource = null;//装载空数据源
            GridView1.DataBind();//绑定数据到显示列表
        }
    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")//查询信息不能为空
        {
            Session["sDL"] == "";
            Session["sTB"] = TextBox1.Text.Trim();
            if (DropDownList1.SelectedIndex == 0)
                Session["sDL"] == "cNo";
            else if (DropDownList1.SelectedIndex = 1)
                Session["sDL"] == "cName";
            else if (DropDownList1.SelectedIndex = 2)
                Session["sDL"] == "cTrem";
            else
                Session["sDL"] == "cPlace";
            bind(Session["sDL"].ToString(), Session["sTB"].ToString());
        }
    }
    protected void ButtonReset_Click(object sender, EventArgs e)
    {
        DropDownList1.SelectedIndex = 0;
        TextBox1.Text = "";
        Label1.Text = "";
        GridView1.DataSource = null;
        GridView1.DataBind();

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
         Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "if(confirm('确定删除？'）){location.href='editCourse.aspx?deleteID="+GridView1 .DataKeys[e.RowIndex ][0].ToString ()+"&pIndex="+GridView1.PageIndex;

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string courseNo = GridView1.DataKeys[e.NewEditIndex][0].ToString();
        string courseName = GridView1.DataKeys[e.NewEditIndex][1].ToString();
        string courseTrem = GridView1.DataKeys[e.NewEditIndex][2].ToString();
        string coursePlace = GridView1.DataKeys[e.NewEditIndex][3].ToString();
        /*当本门课程已经有同学选修，不允许修改课程信息，
         因此必须先判断本门课程是否已经结束；即判断ScoreTb成绩表课程号是否存在，如果存在则说明已经给出分数（课程结束），那就不允许更改。*/
        string mysql = "select * from ScoreTb  where cNo+'" + cNo + "'and sDegree is not null";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataReader myReader = mycmd.ExecuteReader();
        if (myReader.Read())//存在记录
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('本门课程已经有同学选修，不允许修改课程信息！');location.herf='../login.asp'</script>", false);
        else
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>location.herf='editCourseStart.aspx?courseNo=" + courseNo + "&courseName=" + courseName + "&courseTerm=" + courseTerm + "&coursePlace=" + coursePlace + "';</script>", false);

    }
    
      public void delete(string myID)/*删除函数*/
    {
        string mystr = "select * from SelcourseTb  where cNo='" + myID + "'";
        OleDbCommand mycmd = new OleDbCommand(mystr, myConnection);
        OleDbDataReader myReader = mycmd.ExecuteReader();//OleDbDataAdapter表示要对access数据源执行sql语句存储过程
        if (myReader.Read())//存在记录
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('已有学生选修本门课程，不允许删除！')；parent.location.herf='../login.asp'</script>", false);
        else//记录不存在。删除信息
        {
            mystr = "delete * from CourseTb  where cNo='" + myID + "'";
            mycmd = new OleDbCommand(mystr, myConnection);
            mycmd.ExecuteNonQuery();
            GridView1.EditIndex = -1;
        }
        bind(Session["sDL"].ToString(), Session["sTB"].ToString());
        TextBox1.Text = Session["sTB"].ToString();
        if (Session["sDL"].ToString() == "cNo")
            DropDownList1.SelectedIndex = 0;
        else if (Session["sDL"].ToString() == "cName")
            DropDownList1.SelectedIndex = 1;
        else if (Session["sDL"].ToString() == "cTerm")
            DropDownList1.SelectedIndex = 2;
        else
            DropDownList1.SelectedIndex = 3;
    }
    }

}